package com.chamber.web.controller.bussiness;

import com.chamber.business.domain.Machine;
import com.chamber.business.domain.MachineInfo;
import com.chamber.business.mqtt.config.MqttFactory;
import com.chamber.business.service.MachineService;
import com.chamber.common.core.controller.BaseController;
import com.chamber.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 智能氧舱设备控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/machine")
public class MachineController extends BaseController {

    @Autowired
    private MqttFactory mqttFactory;
    @Resource
    private MachineService machineService;

    /**
     * 获取设备列表
     *
     * @return 设备列表
     */
    @GetMapping("/list")
    public AjaxResult getMachineList() {
        List<Machine> machineList = machineService.getMachineList();
        return AjaxResult.success(machineList);
    }

    /**
     * 获取设备状态信息
     *
     * @param machineNum 设备编号
     * @return 设备状态信息
     */
    @GetMapping("/status/{machineNum}")
    public AjaxResult getMachineStatus(@PathVariable("machineNum") Long machineNum) {
        MachineInfo machineInfo = machineService.getMachineInfo(machineNum);
        return AjaxResult.success(machineInfo);
    }

    /**
     * 控制设备开关
     *
     * @param machineNum 设备编号
     * @param status     状态 (1:开启, 0:关闭)
     * @return 操作结果
     */
    @PostMapping("/control/{machineNum}")
    public AjaxResult controlMachine(@PathVariable("machineNum") Long machineNum,
                                     @RequestParam("status") Integer status) {
        try {
            // 构建MQTT控制指令
            String topic = "cmd/set";
            String payload = String.format("{\"machineNum\":%d,\"action\":\"power\",\"value\":%d}",
                    machineNum, status);

            // 通过MQTT发送控制指令
            mqttFactory.publishMessage(topic, payload, 1);

            log.info("发送设备控制指令: {}", payload);
            return AjaxResult.success("指令已发送");
        } catch (Exception e) {
            log.error("发送设备控制指令失败", e);
            return AjaxResult.error("发送控制指令失败");
        }
    }

    /**
     * 调整设备参数
     *
     * @param machineNum 设备编号
     * @param paramType  参数类型 (oxygen:氧气浓度, pressure:气压, volume:音量, light:光照强度)
     * @param value      参数值
     * @return 操作结果
     */
    @PostMapping("/adjust/{machineNum}")
    public AjaxResult adjustMachineParam(@PathVariable("machineNum") Long machineNum,
                                         @RequestParam("paramType") String paramType,
                                         @RequestParam("value") Long value) {
        try {
            // 构建MQTT控制指令
            String topic = "cmd/set";
            String payload = String.format("{\"machineNum\":%d,\"action\":\"%s\",\"value\":%d}",
                    machineNum, paramType, value);

            // 通过MQTT发送控制指令
            mqttFactory.publishMessage(topic, payload, 1);

            log.info("发送设备参数调整指令: {}", payload);
            return AjaxResult.success("参数调整指令已发送");
        } catch (Exception e) {
            log.error("发送设备参数调整指令失败", e);
            return AjaxResult.error("发送参数调整指令失败");
        }
    }

    /**
     * 启动氧疗功能
     *
     * @param machineNum 设备编号
     * @param enable     是否启用 (true:启用, false:停用)
     * @return 操作结果
     */
    @PostMapping("/therapy/{machineNum}")
    public AjaxResult controlOxygenTherapy(@PathVariable("machineNum") Long machineNum,
                                           @RequestParam("enable") Boolean enable) {
        try {
            // 构建MQTT控制指令
            String topic = "cmd/set";
            String payload = String.format("{\"machineNum\":%d,\"action\":\"therapy\",\"value\":%b}",
                    machineNum, enable);
            // 通过MQTT发送控制指令
            mqttFactory.publishMessage(topic, payload, 1);

            log.info("发送氧疗功能控制指令: {}", payload);
            return AjaxResult.success("氧疗功能控制指令已发送");
        } catch (Exception e) {
            log.error("发送氧疗功能控制指令失败", e);
            return AjaxResult.error("发送氧疗功能控制指令失败");
        }
    }


}